﻿using System;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using FranceTriple.Dal;

namespace TeamFranceTriple
{
    public partial class LastPosts : BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<Int32> ForumIdAllowed = UserAccessDal.ForumIdAllowed((Session["AnonymousID"] == null) ? String.Empty : Session["AnonymousID"].ToString());

            if (ForumIdAllowed.Count > 0)
                //Todo : caractère d'échappement pour String.Format ?
                LastPostsEntityDataSource.Where = "it.ForumTopic.ForumCategory.Forum.Id IN {" + String.Join(",", ForumIdAllowed.ToArray()) + "}";
            else
                LastPostsEntityDataSource.Where = "false";
        }

        protected void LastPostsListView_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                FranceTriple.Dal.ForumPost ThisPost = null;
                ThisPost = (FranceTriple.Dal.ForumPost)e.Item.DataItem;

                var targetpostlink = e.Item.FindControl("PostTargetHyperLink") as HyperLink;
                if (targetpostlink != null)
                {
                    targetpostlink.NavigateUrl = String.Format("~/{0}/forumpost/{1}", ThisMaster.Requester.Culture, ThisPost.ForumTopic.Id.ToString());
                }

                var PostMessage_CreationDateLabel = e.Item.FindControl("PostMessage_CreationDateLabel") as Label;
                if (PostMessage_CreationDateLabel != null)
                {
                    PostMessage_CreationDateLabel.Text = ThisPost.PostMessage.CreationDate.ToUserTimeZone(ThisMaster.Requester.Profile.TimeZone).ToString();
                }
            }
        }

    }
}